package com.crawler.waf.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.filter.OncePerRequestFilter;

/**
 * 
 * 跨域资源共享过滤器
 * 
 */
public class CorsFilter extends OncePerRequestFilter {

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	protected void doFilterInternal(HttpServletRequest request,
			HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
			logger.info("CorsFilter doFilter start");
	
			response.addHeader("Access-Control-Allow-Origin", "*");
			response.addHeader("Access-Control-Allow-Methods","GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE, PATCH");
			response.addHeader("Access-Control-Allow-Headers", "Origin, Accept, X_Requested_With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization, Custom");
			response.addHeader("Access-Control-Max-Age", "1800");
			
		
		filterChain.doFilter(request, response);
	}
}
